package org.wlw.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wlw.web.models.Pager;
import org.wlw.web.models.User;
import org.wlw.web.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/admin/user")
@PropertySource("classpath:base.properties")
public class UserController {

    @Value("${uploadPath}")
    private String uploadPath;

    @Autowired
    private IUserService userService;

    @GetMapping("/json/{id}")
    public @ResponseBody
    User getUser(@PathVariable int id) {
        return userService.load(id);
    }

    @GetMapping("/jsons")
    public @ResponseBody
    Pager<User> getUsers() {
        return userService.find(new User());
    }

    @GetMapping("/users")
    public String find(Model model, User searchObj) {
        Pager<User> users = userService.find(searchObj);
        model.addAttribute("datas", users);
        model.addAttribute("searchObj", searchObj);
        return "user/list";
    }

    /*
     * 传递参数的第二种方式
     * */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable int id, Model model) {
        userService.delete(id);
        return "redirect:/admin/user/users";
    }

    @GetMapping("/add")
    public String add(Model model) {
        return "user/add";
    }

    @PostMapping("/add")
    public String add(MultipartFile photo, User user, HttpServletRequest request) {
        try {
            System.out.println(user);
            String oname = photo.getOriginalFilename();
//            String path = request.getSession().getServletContext().getRealPath("/upload");
            String type = oname.substring(oname.lastIndexOf(".") + 1);
            String filepath = uploadPath + "/" + new Date().getTime() + "." + type;
            photo.transferTo(new File(filepath));
//        System.out.println(photo.getOriginalFilename()+"---------------------------------------");
//        System.out.println("-------------------"+request.getSession().getServletContext().getRealPath("/upload"));
//        String otype = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".")+1);
//        String filename = new Date().getTime()+"."+otype;
//        System.out.println("------------------------------"+filename);

            //        userService.add(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/user/users";
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable int id, Model model) {
        User user = userService.load(id);
        model.addAttribute("user", user);
        return "user/update";
    }

    @PostMapping("/update/{id}")
    public String update(@PathVariable int id, User user) {
        User tu = userService.load(id);
        tu.setPassword(user.getPassword());
        tu.setNickname(user.getNickname());
        userService.update(user);
        return "redirect:/admin/user/users";
    }
}
